package com.myhealth.hb.measurmentscanner;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.myhealth.hb.measurmentscanner.com.myhealth.hb.measurmentgenerator.measurements.BloodPressure;

import java.util.Date;
import java.util.Random;


public class BloodPressureGenerator extends Activity {

    private Random generator;
    private ConnectedThread thread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blood_pressure_generator);
        this.generator = new Random();
    }

    @Override
    protected void onResume() {
        super.onResume();
        this.thread = new ConnectedThread(DeviceApplication.socket);
        this.thread.start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if(id == R.id.action_main){
            Intent mainIntent = new Intent(this, Main.class);
            startActivity(mainIntent);
            return true;
        }
        if(id == R.id.action_heartbeat) {
            Intent heartBeatIntent = new Intent(this, HeartBeatGenerator.class);
            startActivity(heartBeatIntent);
            return true;
        }
        if (id == R.id.action_bloodpressure) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private int genRandomBloodPressure(int min, int max){
        return generator.nextInt(max-min) + min;
    }

    public void generate(View view){
        int sys = this.genRandomBloodPressure(90,180);
        int dia = this.genRandomBloodPressure(60,109);
        BloodPressure bloodPressure = new BloodPressure(new Date(), sys,dia);
        Toast.makeText(getApplicationContext(), bloodPressure.toJsonString() , Toast.LENGTH_SHORT).show();
        this.thread.write(bloodPressure.toJsonString().getBytes());
    }
}
